package com.gongpingjialoan.utility;

import android.text.TextUtils;
import android.util.Log;

import com.gongpingjialoan.network.UploadCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2015/11/17.
 */
public class UploadUtils {


    //使用okhttp
    public static void uploadImage(String filePath, final UploadCallBack uploadCallBack, String uri, String prefix) {
        File file = new File(filePath);
        Log.d("upload", "大小：" + file.length());
        RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
        String post_prefix = "";
        if (!TextUtils.isEmpty(prefix))
            post_prefix = ";prefix = \"avatar\"";
//        RequestBody requestBody = new RequestBody() {
//            @Override
//            public MediaType contentType() {
//                return MultipartBuilder.FORM;
//            }
//
//            @Override
//            public void writeTo(BufferedSink sink) throws IOException {
//            }
//
//            @Override
//            public long contentLength() throws IOException {
//                return super.contentLength();
//            }
//        };

        RequestBody multipartBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addPart(Headers.of(
                        "Content-Disposition", "form-data; name=\"image\"; filename=\""
                                + file.getName() + "\"" + post_prefix), fileBody)
                .build();


        Request request = new Request.Builder()
                .url(uri)
                .post(multipartBody)
                .build();
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                uploadCallBack.failure();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("upload", "成功");
                try {

                    if (response.code() == 200) {
                        JSONObject json = new JSONObject(response.body().string());
                        uploadCallBack.success((String) json.get("url"));
                    } else {
                        uploadCallBack.failure();
                    }
                } catch (JSONException e) {
                    uploadCallBack.failure();
                }
            }
        });
    }

}
